<template>
	<view>
		<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm">
			<u-form-item label="主题" prop="title" borderBottom ref="item1">
				<u--input v-model="form.title" placeholder="请输入标题" border="none"></u--input>
			</u-form-item>
			<u-form-item label="内容" prop="content" borderBottom ref="item2">
				<u--textarea v-model="form.content" placeholder="请输入内容"></u--textarea>
			</u-form-item>
			<button @click="submitMsg()">提交</button>
		</u--form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSex: false,

				form: {
					title: '',
					content: ''
				},
				user: {},

				rules: {
					title: {
						type: 'string',
						required: true,
						message: '请填写标题',
						trigger: ['blur', 'change']
					},

					content: [{
						required: true,
						message: '请输入内容',
						trigger: ['blur', 'change']
					}]

				},
				switchVal: false
			};
		},
		methods: {

			submitMsg() {
				this.$refs.uForm.validate().then(res => {
					// uni.$u.toast('校验通过')
					let me = this;
					console.log(this.form)
					console.log(this.user.id)
					this.form.userId = this.user.id
					// console.log(form)
					uni.request({
						url: me.serverUrl + `/messages`, //仅为示例，并非真实接口地址。
						data: this.form,
						method: "POST",
						success: (res) => {
							console.log(res)
							if (res.data.code == "200") {
								uni.showToast({
									title: '反馈成功',
									icon: 'success',
									duration: 2000
								});
								setTimeout(function() {
									uni.reLaunch({
										url: '/pages/center/center'
									})
								}, 2000);
							} else {
								uni.showToast({
									title: '反馈失败',
									icon: 'success',
									duration: 2000
								});
							}
						}
					});
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		},
		onLoad() {
			this.user = uni.getStorageSync('user');
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
	};
</script>

<style lang="scss">

</style>